<template>
  <div class="app-container">
    <el-form :model="queryParams" size='small' ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园区" prop="parkId">
       <el-select
          clearable
          v-model="queryParams.parkId"
          @change="getBuildingList(form.parkId)"
          placeholder="请选择园区"
        >
          <el-option
            v-for="item in buiParkList"
            :key="item.id"
            :label="item.parkNum"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="责任人" prop="liableId">
        <el-select
          clearable
          v-model="queryParams.liableId"
          placeholder="请选择责任人"
        >
          <el-option
            v-for="item in wyUserList"
            :key="item.id"
            :label="item.userName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item> -->



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park:wyClean:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['park:wyClean:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:wyClean:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:wyClean:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wyCleanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="园区" align="center" prop="parkNum" />
      <el-table-column label="保洁位置" align="center" prop="place" />
      <el-table-column label="计划安排" align="center" prop="planTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="userName" />
      <el-table-column label="领料" align="center" prop="material" />
      <el-table-column label="执行结果" align="center" prop="result" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['park:wyClean:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:wyClean:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保洁管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="园区" prop="parkId">
        <el-select
            clearable
            v-model="form.parkId"
            placeholder="请选择园区"
          >
            <el-option
              v-for="item in buiParkList"
              :key="item.id"
              :label="item.parkNum"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保洁位置" prop="place">
          <el-input v-model="form.place" placeholder="请输入保洁位置" />
        </el-form-item>
        <el-form-item label="计划安排" prop="planTime">
         <el-date-picker
            clearable
            size="small"
            v-model="form.planTime"
            type="date"
              value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择计划安排"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="liableId">
        <el-select
            clearable
            v-model="form.liableId"
            placeholder="请选择责任人"
          >
            <el-option
              v-for="item in wyUserList"
              :key="item.id"
              :label="item.userName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领料" prop="material">
          <el-input v-model="form.material" placeholder="请输入领料" />
        </el-form-item>
        <el-form-item label="执行结果" prop="result">
          <el-select v-model="form.result" placeholder="请选择执行结果" clearable size="small">
          <el-option label="已执行" value="已执行" />
          <el-option label="未执行" value="未执行" />
        </el-select>
          <!-- <el-input v-model="form.result" placeholder="请输入执行结果" /> -->
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWyClean, getWyClean, delWyClean, addWyClean, updateWyClean } from "@/api/smartParks/property/propertyCleaning";
import { listBuiPark } from "@/api/parkAssets/enterCommunity";
import { listWyUser } from "@/api/smartParks/property/propertyGreeningPeople";

export default {
  name: "WyClean",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保洁管理表格数据
      wyCleanList: [],
       buiParkList: [],
       wyUserList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkId: null,
        position: null,
        planTime: null,
        liableId: null,
        material: null,
        result: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
       listBuiPark().then((response) => {
      this.buiParkList = response.rows;
    });
     listWyUser({ userType: '保洁' }).then((response) => {
      this.wyUserList = response.rows;
    });
  },
  methods: {
    /** 查询保洁管理列表 */
    getList() {
      this.loading = true;
      listWyClean(this.queryParams).then(response => {
        this.wyCleanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parkId: null,
        position: null,
        planTime: null,
        liableId: null,
        material: null,
        result: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,


      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保洁管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWyClean(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保洁管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWyClean(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWyClean(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除保洁管理的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWyClean(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('park/wyClean/export', {
        ...this.queryParams
      }, `park_wyClean.xlsx`)
    }
  }
};
</script>
